﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using ProductIntroduction.Models;

namespace ProductIntroduction.Areas.Admin.Controllers
{ 
    public class ConfigController : Controller
    {
        private DBEntities db = new DBEntities();

        //
        // GET: /Admin/Config/

        public ViewResult Index()
        {
            return View(db.Configs.ToList());
        }


        //
        // GET: /Admin/Config/Create
        [HttpGet]
        public ActionResult Create()
        {

            return View();
        }

        //
        // POST: /Admin/Category/Create

        [HttpPost]
        public ActionResult CreateConfig()
        {
            var websiteTitle = Request["WebsiteTitle"];
            var websiteName = Request["WebsiteName"];
            var websiteSlogan = Request["WebsiteSlogan"];
            var bannerUrl = Request["Banner"];
            var footer = Request["Footer"];

            var objConfig = new Config
                                {
                                    WebsiteTitle = websiteTitle,
                                    WebsiteName = websiteName,
                                    WebsiteSlogan = websiteSlogan,
                                    Banner = bannerUrl,
                                    Footer = footer,
                                    Selected = true
                                };

            db.Configs.ToList().ForEach(c => c.Selected = false);
            db.Configs.AddObject(objConfig);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        
        //
        // GET: /Admin/Config/Edit/5

        public ActionResult Edit(int id)
        {
            Config config = db.Configs.Single(s => s.ConfigId == id);

            return View(config);
        }

        //
        // POST: /Admin/Config/Edit/5

        [HttpPost]
        public ActionResult Edit(Config config)
        {
            if (ModelState.IsValid)
            {
                db.Configs.Attach(config);
                db.ObjectStateManager.ChangeObjectState(config, EntityState.Modified);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(config);
        }

        [HttpPost]
        public ContentResult Upload()
        {
            var file = Request.Files[0];
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = DateTime.Now.ToString("yyyyMMdd_HHmmss_") + Path.GetFileName(file.FileName);
                // store the file inside ~/Upload/Banner folder
                var path = Path.Combine(Server.MapPath("~/Upload/Banner/"), fileName);
                file.SaveAs(path);

                var fileUrl = string.Empty;
                if (Request.Url.Port == 0)
                {
                    fileUrl = "http://" + Request.Url.Host + "/Upload/Banner/" + fileName;
                }
                else
                {
                    fileUrl = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Upload/Banner/" + fileName;
                }

                return Content("{ FilePath = '" + fileUrl + "', FileName = '" + fileName + "' }");
            }

            return Content("{ Success = false }");
        }

        //
        // GET: /Admin/Category/Delete/5

        public ActionResult Delete(int id)
        {
            Config config = db.Configs.Single(c => c.ConfigId == id);

            return View(config);
        }

        //
        // POST: /Admin/Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Config config = db.Configs.Single(c => c.ConfigId == id);
            db.Configs.DeleteObject(config);
            db.SaveChanges();

            return RedirectToAction("Index");
        }


        //
        // POST: /Admin/Config/SelectConfig/5
        [HttpPost]
        public JsonResult SelectConfig(int id)
        {
            db.Configs.ToList().ForEach(c => c.Selected = false);
            Config config = db.Configs.Single(s => s.ConfigId == id);
            config.Selected = true;
            db.SaveChanges();

            return Json(new ResultModel {Success = true, Message = "Update successfully"});
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}